package com.example.newsapp.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

import com.example.newsapp.R;
import com.example.newsapp.fragment.UnloggedFragment;
import com.example.newsapp.fragment.NewsFragment;
import com.example.newsapp.fragment.ProfileFragment;
import com.example.newsapp.utils.UserSessionUtil;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;
    private Fragment newsFragment;
    private Fragment profileFragment;
    private Fragment unloggedFragment;
    private Fragment activeFragment;
    private FragmentManager fragmentManager;
    private MenuItem profileMenuItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
        setupFragments();
        setupBottomNavigation();

        // 默认显示新闻页面
        switchFragment(newsFragment);
        // 更新底部导航栏文字
        updateProfileMenuTitle();
    }

    private void initViews() {
        bottomNavigationView = findViewById(R.id.bottomNavigation);
        fragmentManager = getSupportFragmentManager();
        profileMenuItem = bottomNavigationView.getMenu().findItem(R.id.navigation_profile);
    }

    private void setupFragments() {
        // 初始化fragments
        newsFragment = new NewsFragment();
        profileFragment = new ProfileFragment();
        unloggedFragment = new UnloggedFragment();

        // 添加所有fragment
        fragmentManager.beginTransaction()
                .add(R.id.fragmentContainer, profileFragment)
                .hide(profileFragment)
                .add(R.id.fragmentContainer, unloggedFragment)
                .hide(unloggedFragment)
                .add(R.id.fragmentContainer, newsFragment)
                .commit();

        activeFragment = newsFragment;
    }

    private void setupBottomNavigation() {
        bottomNavigationView.setOnItemSelectedListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.navigation_news) {
                // 新闻页面所有人都可以看
                switchFragment(newsFragment);
                return true;
            } else if (itemId == R.id.navigation_profile) {
                // 只有点击"未登录/我的"时 判断登录状态
                if (UserSessionUtil.isLoggedIn(this)) {
                    switchFragment(profileFragment);
                } else {
                    switchFragment(unloggedFragment);
                }
                return true;
            }
            return false;
        });
    }

    private void switchFragment(Fragment targetFragment) {
        if (activeFragment != targetFragment) {
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.hide(activeFragment).show(targetFragment).commit();
            activeFragment = targetFragment;
        }
    }

    //判断用户是否登录来更新导航栏文字
    private void updateProfileMenuTitle() {
        if (UserSessionUtil.isLoggedIn(this)) {
            profileMenuItem.setTitle("我的");
        } else {
            profileMenuItem.setTitle("未登录");
        }
    }

    // 处理退出登录
    public void onLogout() {
        // 更新底部导航栏文字
        updateProfileMenuTitle();
        // 切换到未登录页面
        switchFragment(unloggedFragment);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 更新底部导航栏文字
        updateProfileMenuTitle();

        // 如果当前在个人页面，根据登录状态切换显示
        if (activeFragment == profileFragment || activeFragment == unloggedFragment) {
            if (UserSessionUtil.isLoggedIn(this)) {
                switchFragment(profileFragment);
            } else {
                switchFragment(unloggedFragment);
            }
        }
    }

}